package net.hzlj.platform.enterprise.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.orhanobut.logger.Logger;

import net.hzlj.platform.enterprise.AppManager;
import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.beans.UserBean;
import net.hzlj.platform.enterprise.beans.resp.RespBaseBean;
import net.hzlj.rl.zhangjiajie.R;
import net.hzlj.platform.enterprise.config.HttpConfig;
import net.hzlj.platform.enterprise.helper.ToastHelper;
import net.hzlj.platform.enterprise.task.HttpRequestApi;
import net.hzlj.platform.enterprise.task.Task;
import net.hzlj.platform.enterprise.utils.AppPreferencesUtils;
import net.hzlj.platform.enterprise.utils.PreferencesUtils;
import net.hzlj.platform.enterprise.utils.StringUtils;
import net.hzlj.platform.enterprise.widght.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangePasswordActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.old_password_editText)
    EditText mOldPasswordEditText;
    @BindView(R.id.new_password_editText)
    EditText mNewPasswordEditText;
    @BindView(R.id.confirm_button)
    Button mConfirmButton;
    private MyHandler<ChangePasswordActivity> mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setRootView() {
        super.setRootView();
        setContentView(R.layout.activity_change_password);
    }

    @Override
    protected void initTitleBar() {
        super.initTitleBar();
        mHandler = new MyHandler<ChangePasswordActivity>(this);
        mTitleBar.bindActivity(this);
        mTitleBar.setOnMainRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }, "保存", 0, View.INVISIBLE);
        mTitleBar.setOnMainLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }, "返回", 0, View.VISIBLE);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {

    }

    @OnClick(R.id.confirm_button)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirm_button:
                if (BootApp.getInstance().netIsConnect(ChangePasswordActivity.this)) {
                    changePassword();
                }
                break;
        }
    }


    private void changePassword(){
        UserBean userBean = AppPreferencesUtils.getObject(this,PreferencesUtils.KEY_USER_INFO,UserBean.class);
        if (userBean == null){
            ToastHelper.newInstance().show(this,"请预先登录");
            startActivity(new Intent(ChangePasswordActivity.this,LoginActivity.class));
            return ;
        }
        String oldPwd = mOldPasswordEditText.getText().toString();
        String newPwd = mNewPasswordEditText.getText().toString();
        if (StringUtils.isEmpty(oldPwd)){
            ToastHelper.newInstance().show(this,"原始密码不能为空");
            return;
        }
        if (StringUtils.isEmpty(newPwd)){
            ToastHelper.newInstance().show(this,"新密码不能为空");
            return;
        }
        mLoadingDialog.show("密码修改中...",false);
        HttpRequestApi.changePassWord(userBean.getIdentityNO(),oldPwd,newPwd,this,this);
    }

    @Override
    public void onTaskResult(Task task) {
        super.onTaskResult(task);
        if (task.type == HttpConfig.Type.CHANGE_PASSWORD){
            if (mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess){
                RespBaseBean<UserBean> respBaseBean = task.resData;
                if (respBaseBean.isSuccess()){
                    UserBean userBean = respBaseBean.getValues().get(0);
                    AppPreferencesUtils.putObject(ChangePasswordActivity.this, PreferencesUtils.KEY_USER_INFO, userBean);
                    AppPreferencesUtils.putString(ChangePasswordActivity.this, PreferencesUtils.KEY_USER_ACCOUNT, userBean.getIdentityNO());
                    finish();
                }
            }
            ToastHelper.newInstance().show(this,task.rspDesc);
        }
    }
}
